<?php

class Admin_GameController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    private $_memcache;
    public function init(){
        $acl                = new My_Acl();
        $auth               = $acl->checkAllowed($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
    }
    public function getgameAction()
    {
        $this->getHelper('Layout')->disableLayout();
        $this->getHelper('ViewRenderer')->setNoRender();
        $partnerId=$this->_getParam('partnerId');
        if(!isset($partnerId) && !$partnerId){
              $partnerId=0;
        }
        $modelGame=new Admin_Model_Game($partnerId);
        $result=$modelGame->getAllData();
        echo json_encode($result->toArray());
    }
    public function indexAction(){
        $modelRole = new Default_Model_Role();
        $roleCheck = $modelRole->getDataByUserId($this->_auth['userId']);
       // pr($roleCheck->toArray());
        $gameList=array();
        $Id = $this->_getParam('partnerId');;
        foreach($roleCheck as $row){ 
            $gameId=json_decode($row['gameId'],True);
            //var_dump($gameId);
            foreach($gameId as $key=> $value){
                if($key==$Id){
                   foreach($value as $val){
                      $gameList[]=$val;
                   }    
                }    
            }
        }
      
        $gameList=array_unique($gameList);
        $gameList = '("'.implode('","',$gameList).'")';
        $modelGame = new Admin_Model_Game($Id);
        $gameList = $modelGame->getDataByGameList($gameList);
        
        if(isset($gameList)){
            $this->view->gameList = $gameList->toArray();
            //pr($partnerList->toArray());
        }
        
    }
   
    public function deleteAction(){
        $partnerId = $this->_getParam('partnerId');
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            
            $params=array('partnerId'=>$partnerId);
            if($del == 'Có'){
                $gameId = $this->getRequest()->getPost('gameId');
                $modelGame = new Admin_Model_Game($partnerId);
                $modelGame->deleteData($gameId);
            }
            $this->_helper->redirector('index', 'game', 'admin', $params);
        }else{
            $id = $this->_getParam('id', 0);
            $modelGame = new Admin_Model_Game($partnerId);
            $this->view->gameCheck = $modelGame->getDataById($id);
        }
    }
    public function addAction(){
        $Id = $this->_getParam('partnerId');
        $form = new Admin_Form_Game();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $params=array('partnerId'=>$Id);
                $gameId     = $form->getValue('gameId');
                //$catId      = $form->getValue('catId');
                $catId      = 0;
                $openDate   = $form->getValue('openDate');
                $gameName   = $form->getValue('gameName');
                $level      = $form->getValue('level');
                $status     = $form->getValue('status');
                //$gameConfig = $form->getValue('gameConfig');
                
                $upload = new Zend_File_Transfer;
                $upload->setDestination(PUBLIC_PATH.'/assets/');
                $files = $upload->getFileInfo();
                $upload->addFilter('Rename', date("Y-m-d-H-i-s-").$upload->getFileName(null,false));
                $upload->receive();
                if($upload->getFileName(null,false)){
                    $gameImg = '/assets/'.$upload->getFileName(null,false);
                }else{
                    $gameImg = '';
                }
                $modelGame = new Admin_Model_Game($Id);
               
                $gameId = $modelGame->addData($gameId, $gameName, $catId, $gameImg, $this->_helper->time->dateToData($openDate), $this->_auth['userId'],date("Y-m-d"), $level, $status);
//                if($gameId){
//                    $file = fopen(CONFIG_PATH.'/'.$gameId.'.ini', 'w'); 
//                    fwrite($file, $gameConfig);
//                    fclose($file);
//                }
                        
                $this->_helper->redirector('index', 'game', 'admin', $params);

            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction(){
		$form = new Admin_Form_Game();
		$form->submit->setLabel('Lưu');
		$this->view->form = $form;
        $id = $this->_getParam('id', 0);
		$partnerId = $this->_getParam('partnerId', 0);
        $params = array('partnerId' => $partnerId);
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();            
			if($form->isValid($formData)){
                $modelGame = new Admin_Model_Game($partnerId);
    			$gameCheck = $modelGame->getDataByID($id);
                $gameId     = $form->getValue('gameId');
                $openDate   = $form->getValue('openDate');
                $gameName   = $form->getValue('gameName');
                $status     = $form->getValue('status');
                //$gameConfig = $form->getValue('gameConfig');
                
                $repair     = $this->_getParam('resourceId');

                $upload = new Zend_File_Transfer;
                $upload->setDestination(PUBLIC_PATH.'/assets/');
                $files = $upload->getFileInfo();
                $upload->addFilter('Rename', date("Y-m-d-H-i-s-").$upload->getFileName(null,false));
                $upload->receive();
                if($upload->getFileName(null,false)){
                    $gameImg = '/assets/'.$upload->getFileName(null,false);
                    unlink(ROOT_PATH."/public".$gameCheck['gameImg']);
                }else{
                    $gameImg = $gameCheck['gameImg'];
                }
                
                $gameCheck = $modelGame->getDataByGameName($gameName);
                //$gameId, $gameName, $catId, $gameImg, $openDate, $repair = '', $level, $status
                if(!$gameCheck || $gameCheck['gameId'] == $gameId){
                    $modelGame->updateData($id, $gameName,$catId=0, $gameImg, $this->_helper->time->dateToData($openDate), json_encode($repair), $status);
//                    if($gameId){
//                        $file = fopen(CONFIG_PATH.'/'.$gameId.'.ini', 'w'); 
//                        fwrite($file, $gameConfig);
//                        fclose($file);
//                    }
                    $this->_helper->redirector('index', 'game', 'admin', $params);
                }else{
                    $this->view->mess ="Tên game đã tồn tại!!!";
                    $this->view->flag = "alert";
    				$form->populate($formData);
    			}
			}else{
				$form->populate($formData);
			}
		}else{
            $modelGame = new Admin_Model_Game($partnerId);
			$gameCheck = $modelGame->getDataByID($id)->toArray();
            if($gameCheck){
    			$this->view->gameCheck = $gameCheck;
                if(isset($gameCheck['openDate'])){
                    $gameCheck['openDate'] = $this->_helper->time->dataToDate($gameCheck['openDate']);
                }
    			$form->populate($gameCheck);
                                
                
            }else{
                $this->_helper->redirector('index', 'game', 'admin' ,$params);
            }
		}
    }
    
}